#coding:utf-8
import appconf,hashlib,time
import datetime,uuid
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(appconf.app)

class Md5():
    def _file_as_bytes(self, file):
        with file:
            return file.read()

    @staticmethod
    def md5_string(str):
        """对字符串进行md5"""
        hl = hashlib.md5()
        hl.update(str.encode(encoding='utf-8'))
        return hl.hexdigest()

    @staticmethod
    def md5_file(file):
        """一般文件md5签名"""
        return hashlib.md5(Md5()._file_as_bytes(open(file, 'rb'))).hexdigest()

class User(db.Model):
    __tablename__ = "user_info"
    id = db.Column(db.Integer, primary_key=True)
    userName = db.Column(db.String(50), nullable=True)
    passWord = db.Column(db.String(100), nullable=True)

    openId = db.Column(db.String(100), nullable=True)
    token = db.Column(db.String(100), nullable=True)
    isShow = db.Column(db.Integer, nullable=True)
    role = db.Column(db.Integer, nullable=True)
    #   拥有那些项目的权限
    project_id = db.Column(db.String(200), nullable=True)
    last_time = db.Column(db.String(50), nullable=True)

    def add(self):
        db.session.add(self)
        db.session.commit()
        return self.id

    def __init__(self,userName,passWord,project_id,role,isShow=0):
        self.userName = userName
        self.passWord = Md5.md5_string(passWord)
        self.project_id = project_id
        self.openId = Md5.md5_string("%s%s"%(self.id,self.userName))
        self.isShow = isShow
        self.role = role
        self.token = Md5.md5_string("%s" % int(time.time()))
        self.last_time = str(datetime.datetime.now())

db.create_all()


